package com.device.search.jframe;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.device.search.model.DeviceInit;
import com.device.search.service.ConstructionBasicService;
import com.device.search.utils.SpringUtil;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;

@Component
public class DeviceJFrame extends JFrame implements ActionListener {

	@Autowired
	private ConstructionBasicService constructionBasicService;

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTable table;

	public static final String[] TABLE_HEAD = { "项目ID", "设备ID", "项目名称", "设备编号", "设备状态", "移动号码" };
	public Object[][] tableBody = new Object[20][6];

	/**
	 * Create the frame.
	 */
	public DeviceJFrame() {
		setTitle("首页");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1050, 702);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		DefaultTableModel tableModel = new DefaultTableModel(tableBody, TABLE_HEAD);

		JButton deviceDetails = new JButton("设备详情查询");
		deviceDetails.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DeviceDetailsJFrame frame = SpringUtil.getBean(DeviceDetailsJFrame.class);
				frame.setVisible(true);
				dispose();
			}
		});
		deviceDetails.setBounds(14, 29, 181, 49);
		contentPane.add(deviceDetails);

		JButton deviceStatus = new JButton("设备状态查询");
		deviceStatus.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DeviceStatusJFrame frame = SpringUtil.getBean(DeviceStatusJFrame.class);
				frame.setVisible(true);
				dispose();
			}
		});
		deviceStatus.setBounds(300, 29, 181, 49);
		contentPane.add(deviceStatus);

		JButton deviceData = new JButton("设备数据查询");
		deviceData.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DeviceDataJFrame frame = SpringUtil.getBean(DeviceDataJFrame.class);
				frame.setVisible(true);
				dispose();
			}
		});
		deviceData.setBounds(577, 29, 181, 49);
		contentPane.add(deviceData);

		JButton btnNewButton = new JButton("查询离线设备");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				List<DeviceInit> list = constructionBasicService.findDeviceStatus();
				Object[][] tableBody = new Object[list.size()][6];
				for (int i = 0; i < list.size(); i++) {
					tableBody[i][0] = list.get(i).getConstruction_id();
					tableBody[i][1] = list.get(i).getId();
					tableBody[i][2] = list.get(i).getProject_name();
					tableBody[i][3] = list.get(i).getDvice_no();
					tableBody[i][4] = list.get(i).getPid() == 0 ? "离线" : "在线";
					tableBody[i][5] = list.get(i).getMobile();
				}
				tableModel.setDataVector(tableBody, TABLE_HEAD);
			}
		});
		btnNewButton.setBounds(837, 29, 181, 49);
		contentPane.add(btnNewButton);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(14, 103, 1004, 539);
		contentPane.add(scrollPane);

		table = new JTable();
		table.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		table.setRowHeight(30);
		table.setModel(tableModel);
		DefaultTableCellRenderer r = new DefaultTableCellRenderer();
		r.setHorizontalAlignment(JLabel.CENTER);
		table.setDefaultRenderer(Object.class, r);
		scrollPane.setColumnHeaderView(table);
		scrollPane.setViewportView(table);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

	}
}
